ClassicNoise2D.hlsl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // GLSL textureless classic 2D noise "cnoise",
  3. // with an RSL-style periodic variant "pnoise".
  4. // Author: Stefan Gustavson (stefan.gustavson@liu.se)
  5. // Version: 2011-08-22
  6. //
  7. // Many thanks to Ian McEwan of Ashima Arts for the
  8. // ideas for permutation and gradient selection.
  9. //
  10. // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
  11. // Distributed under the MIT license. See LICENSE file.
  12. // https://github.com/ashima/webgl-noise
  13. //
  14. #ifndef _INCLUDE_JP_KEIJIRO_NOISESHADER_CLASSIC_NOISE_2D_HLSL_
  15. #define _INCLUDE_JP_KEIJIRO_NOISESHADER_CLASSIC_NOISE_2D_HLSL_
  16. #include "Common.hlsl"
  17. float ClassicNoise_impl(float2 pi0, float2 pf0, float2 pi1, float2 pf1)
  18. {
  19. pi0 = wglnoise_mod289(pi0); // To avoid truncation effects in permutation
  20. pi1 = wglnoise_mod289(pi1);
  21. float4 ix = float2(pi0.x, pi1.x).xyxy;
  22. float4 iy = float2(pi0.y, pi1.y).xxyy;
  23. float4 fx = float2(pf0.x, pf1.x).xyxy;
  24. float4 fy = float2(pf0.y, pf1.y).xxyy;
  25. float4 i = wglnoise_permute(wglnoise_permute(ix) + iy);
  26. float4 phi = i / 41 * 3.14159265359 * 2;
  27. float2 g00 = float2(cos(phi.x), sin(phi.x));
  28. float2 g10 = float2(cos(phi.y), sin(phi.y));
  29. float2 g01 = float2(cos(phi.z), sin(phi.z));
  30. float2 g11 = float2(cos(phi.w), sin(phi.w));
  31. float n00 = dot(g00, float2(fx.x, fy.x));
  32. float n10 = dot(g10, float2(fx.y, fy.y));
  33. float n01 = dot(g01, float2(fx.z, fy.z));
  34. float n11 = dot(g11, float2(fx.w, fy.w));
  35. float2 fade_xy = wglnoise_fade(pf0);
  36. float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
  37. float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
  38. return 1.44 * n_xy;
  39. }
  40. // Classic Perlin noise
  41. float ClassicNoise(float2 p)
  42. {
  43. float2 i = floor(p);
  44. float2 f = frac(p);
  45. return ClassicNoise_impl(i, f, i + 1, f - 1);
  46. }
  47. // Classic Perlin noise, periodic variant
  48. float PeriodicNoise(float2 p, float2 rep)
  49. {
  50. float2 i0 = wglnoise_mod(floor(p), rep);
  51. float2 i1 = wglnoise_mod(i0 + 1, rep);
  52. float2 f = frac(p);
  53. return ClassicNoise_impl(i0, f, i1, f - 1);
  54. }
  55. #endif